home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_110 / mint / bash110s.zoo / bash-1.10 / builtins / echo.def < prev    next >
Encoding:
Text File  |  1991-07-09  |  3.5 KB  |  148 lines

  1. This file is echo.def, from which is created echo.c.
  2. It implements the builtin "echo" in Bash.
  3.  
  4. Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7.  
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 1, or (at your option) any later
  11. version.
  12.  
  13. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License along
  19. with Bash; see the file COPYING.  If not, write to the Free Software
  20. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. $PRODUCES echo.c
  23. #include <stdio.h>
  24. #include "../shell.h"
  25.  
  26. $BUILTIN echo
  27. $FUNCTION echo_builtin
  28. $DEPENDS_ON V9_ECHO
  29. $SHORT_DOC echo [-n] [-e] [arg ...]
  30. Output the ARGs.  if -n is specified, the trailing newline is
  31. suppressed.  If the -e option is given, interpretation of the
  32. following backslash-escaped characters is turned on:
  33.     \a    alert (bell)
  34.     \b    backspace
  35.     \c    suppress trailing newline
  36.     \f    form feed
  37.     \n    new line
  38.     \r    carriage return
  39.     \t    horizontal tab
  40.     \v    vertical tab
  41.     \\    backslash
  42.     \num    the character whose ASCII code is NUM (octal).
  43. $END
  44.  
  45. $BUILTIN echo
  46. $FUNCTION echo_builtin
  47. $DEPENDS_ON !V9_ECHO
  48. $SHORT_DOC echo [-n] [arg ...]
  49. Output the ARGs.  If -n is specified, the trailing newline is suppressed.
  50. $END
  51.  
  52. /* Print the words in LIST to standard output.  If the first word is
  53.    `-n', then don't print a trailing newline.  We also support the
  54.    echo syntax from Version 9 unix systems. */
  55. echo_builtin (list)
  56.      WORD_LIST *list;
  57. {
  58.   int display_return = 1, do_v9 = 0;
  59.  
  60. /* System V machines already have a /bin/sh with a v9 behaviour.  We
  61.    give Bash the identical behaviour for these machines so that the
  62.    existing system shells won't barf. */
  63. #if defined (V9_ECHO) && defined (USG)
  64.     do_v9 = 1;
  65. #endif
  66.  
  67.   while (list && list->word->word[0] == '-')
  68.     {
  69.       char *temp = &(list->word->word[1]);
  70.  
  71.       if (!*temp)
  72.     goto just_echo;
  73.  
  74.       while (*temp)
  75.     {
  76.       if (*temp == 'n')
  77.         display_return = 0;
  78. #if defined (V9_ECHO)
  79.       else if (*temp == 'e')
  80.         do_v9 = 1;
  81. #if defined (USG)
  82.       else if (*temp == 'E')
  83.         do_v9 = 0;
  84. #endif /* USG */
  85. #endif /* V9_ECHO */
  86.       else
  87.         goto just_echo;
  88.  
  89.       temp++;
  90.     }
  91.       list = list->next;
  92.     }
  93.  
  94. just_echo:
  95.  
  96.   if (list)
  97.     {
  98. #if defined (V9_ECHO)
  99.       if (do_v9)
  100.     {
  101.       while (list)
  102.         {
  103.           register char *s = list->word->word;
  104.           register int c;
  105.  
  106.           while (c = *s++)
  107.         {
  108.           if (c == '\\' && *s)
  109.             {
  110.               switch (c = *s++)
  111.             {
  112.             case 'a': c = '\007'; break;
  113.             case 'b': c = '\b'; break;
  114.             case 'c': display_return = 0; continue;
  115.             case 'f': c = '\f'; break;
  116.             case 'n': c = '\n'; break;
  117.             case 'r': c = '\r'; break;
  118.             case 't': c = '\t'; break;
  119.             case 'v': c = (int) 0x0B; break;
  120.             case '0': case '1': case '2': case '3':
  121.             case '4': case '5': case '6': case '7':
  122.               c -= '0';
  123.               if (*s >= '0' && *s <= '7')
  124.                 c = c * 8 + (*s++ - '0');
  125.               if (*s >= '0' && *s <= '7')
  126.                 c = c * 8 + (*s++ - '0');
  127.               break;
  128.             case '\\': break;
  129.             default:  putchar ('\\'); break;
  130.             }
  131.             }
  132.           putchar(c);
  133.         }
  134.           list = list->next;
  135.           if (list)
  136.         putchar(' ');
  137.         }
  138.     }
  139.       else
  140. #endif /* V9_ECHO */
  141.     print_word_list (list, " ");
  142.     }
  143.   if (display_return)
  144.     printf ("\n");
  145.   fflush (stdout);
  146.   return (EXECUTION_SUCCESS);
  147. }
  148.